package com.eclipse.e20200117_周考_servlet_curd.com.qf.crud.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.eclipse.e20200117_周考_servlet_curd.com.qf.crud.dao.StudentDao;

@WebServlet("/servletStudentSelect")
public class StudentServletSelect extends HttpServlet{
	
	private static Logger logger = LogManager.getLogger();
	private static StudentDao dao = new StudentDao();
	private static ObjectMapper objectMapper = new ObjectMapper();
	private static List<Student> list=null;
	String studentsJSON=null;
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		response.setContentType("text/json;charset=utf-8");
		PrintWriter out = response.getWriter();
		
		list = dao.select(Student.class);//传一个Class类对象
		studentsJSON = objectMapper.writeValueAsString(list);
		logger.trace(studentsJSON); 
		out.print(studentsJSON);
	}
}
